package jmine.tec.report.cell.impl;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.NumberFormat;

import jmine.tec.report.cell.FormattedCell;

/**
 * @author adalberto
 */
public class DoubleDataCell extends AbstractDoubleValueDataCell implements Serializable, FormattedCell {

    private static final double MENOS_ZERO = -0.0;

    private static final long serialVersionUID = 2491173309184820961L;

    private final NumberFormat nf;

    /**
     * Construtor
     * 
     * @param value Double
     * @param nf the {@link NumberFormat}
     */
    public DoubleDataCell(final Double value, NumberFormat nf) {
        super(value);
        this.nf = nf;
    }

    /**
     * Construtor
     * 
     * @param value Double
     */
    public DoubleDataCell(final Double value) {
        this(value, null);
    }

    /**
     * C'tor with primitive param
     * 
     * @param value the value
     * @param nf {@link NumberFormat}
     */
    public DoubleDataCell(final double value, NumberFormat nf) {
        super(value == MENOS_ZERO ? 0.0 : value);
        this.nf = nf;
    }

    /**
     * C'tor with primitive param
     * 
     * @param value the value
     */
    public DoubleDataCell(final double value) {
        this(value, null);
    }

    /**
     * Retorna uma sitrng no formato doubleFormat recebido
     * 
     * @param dateFormat DateFormat
     * @param doubleFormat NumberFormat
     * @param currencyFormat NumberFormat
     * @return String
     */
    public String toString(final DateFormat dateFormat, final NumberFormat doubleFormat, final NumberFormat currencyFormat) {
        final Double toDouble = this.toDouble();
        if (toDouble.isNaN()) {
            return "NaN";
        }
        if (doubleFormat == null) {
            return String.valueOf(toDouble);
        }
        return doubleFormat.format(toDouble);
    }

    /**
     * {@inheritDoc}
     */
    public String format() {
        if (this.nf != null) {
            return this.nf.format(this.toDouble().doubleValue());
        } else {
            return this.toDouble().toString();
        }
    }

    /**
     * @return the nf
     */
    public NumberFormat getNumberFormat() {
        return this.nf;
    }

}
